package com.dogegg.lbbopenapi.security;

import java.util.HashMap;

public class Encrypter {

    private static final HashMap<String, Strategy> factory = new HashMap<>();

    public static final String RSA = "rsa";
    public static final String AES = "aes";
    public static final String SM2 = "sm2";
    public static final String SM4 = "sm4";

    static {
        factory.put(RSA,new RSA());
        factory.put(AES,new AES());
        factory.put(SM2,new SM2());
        factory.put(SM4,new SM4());
    }

    static Strategy getStrategy(String key){
        String type = key.toLowerCase();
        Strategy strategy = factory.get(type);
        if(strategy == null){
            throw new IllegalArgumentException("encryptType must [rsa,aes1]");
        }
        return strategy;
    }

   public static String  encrypt (String encryptType,String content,String key) {
      return getStrategy(encryptType).encrypt(content,key);
   }

   public  static String decrypt (String encryptType,String content,String key)  {
       return getStrategy(encryptType).decrypt(content,key);
   }

    interface Strategy {
        String encrypt (String content,String key) ;
        String decrypt (String content,String key) ;
    }

    static class RSA implements Strategy {
        @Override
        public String encrypt(String content, String key) {
            try {
                throw new UnsupportedOperationException("RSA encrypt not supported");
            }catch (Exception e){
                throw new RuntimeException(e);
            }

        }

        @Override
        public String decrypt(String content, String key)  {
            try {
                throw new UnsupportedOperationException("RSA decrypt not supported");
            }catch (Exception e){
                throw new RuntimeException(e);
            }
        }
    }

    static class AES implements Strategy {
        @Override
        public String encrypt(String content, String key) {
            String secret = key.substring(0, 24);
            String iv  = key.substring(24);
            throw new UnsupportedOperationException("AES encrypt not supported");
        }

        @Override
        public String decrypt(String content, String key)  {
            String secret = key.substring(0, 24);
            String iv  = key.substring(24);
            throw new UnsupportedOperationException("AES decrypt not supported");
        }
    }

    static class SM2 implements Strategy {
        @Override
        public String encrypt(String content, String key) {
            throw new UnsupportedOperationException("SM2 encrypt not supported");
        }

        @Override
        public String decrypt(String content, String key)  {
          throw new UnsupportedOperationException("SM2 decrypt not supported");
        }
    }

    static class SM4 implements Strategy {
        @Override
        public String encrypt(String content, String key) {
          throw new UnsupportedOperationException("SM4 encrypt not supported");
        }

        @Override
        public String decrypt(String content, String key)  {
          throw new UnsupportedOperationException("SM4 decrypt not supported");
        }
    }


}
